package com.android.phoenix.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.Toast;

import com.android.phoenix.R;
import com.android.phoenix.fragments.FragmentSwitchListener;
import com.android.phoenix.fragments.userinfoimprove.UserInfoAgeFragment;
import com.android.phoenix.fragments.userinfoimprove.UserInfoHeightFragment;
import com.android.phoenix.fragments.userinfoimprove.UserInfoSexFragment;
import com.android.phoenix.fragments.userinfoimprove.UserInfoWeightFragment;

/**
 * @ClassName: UserInfoImproveActivity
 * @Description: 用户第一次登录信息完善
 * @author: wanchao
 * @date: 2015-1-7 下午10:12:40
 */
public class UserInfoImproveActivity extends FragmentActivity implements
		FragmentSwitchListener {

	private Fragment fraUserInfoSex = null;
	private Fragment fraUserInfoAge = null;
	private Fragment fraUserInfoHeight = null;
	private Fragment fraUserInfoWeight = null;

	private long back = 0;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.acti_empty);

		fraUserInfoSex = new UserInfoSexFragment();
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.layout_container, fraUserInfoSex).commit();
	}

	@Override
	public void fragmentSwitch(String classNameHide, String classNameShow,
			Bundle bundle) {
		// TODO Auto-generated method stub
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		if (FragmentSwitchListener.ACTIVITY_FINISH.equals(classNameShow)) {
			Intent it = new Intent(UserInfoImproveActivity.this,
					MainActivity.class);
			startActivity(it);
			overridePendingTransition(R.anim.slide_bottom_in,
					R.anim.slide_top_out);
			UserInfoImproveActivity.this.finish();
		} else if (UserInfoSexFragment.class.getName().equals(classNameShow)) {
			if (fraUserInfoSex == null) {
				fraUserInfoSex = new UserInfoSexFragment();
			}
			ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_right_out)
					.replace(R.id.layout_container, fraUserInfoSex).commit();
		} else if (UserInfoAgeFragment.class.getName().equals(classNameShow)) {
			if (fraUserInfoAge == null) {
				fraUserInfoAge = new UserInfoAgeFragment();
			}
			if (UserInfoSexFragment.class.getName().equals(classNameHide)) {
				ft.setCustomAnimations(R.anim.slide_right_in,
						R.anim.slide_left_out);
			} else {
				ft.setCustomAnimations(R.anim.slide_left_in,
						R.anim.slide_right_out);
			}
			ft.replace(R.id.layout_container, fraUserInfoAge).commit();
		} else if (UserInfoHeightFragment.class.getName().equals(classNameShow)) {
			if (fraUserInfoHeight == null) {
				fraUserInfoHeight = new UserInfoHeightFragment();
			}
			if (UserInfoAgeFragment.class.getName().equals(classNameHide)) {
				ft.setCustomAnimations(R.anim.slide_right_in,
						R.anim.slide_left_out);
			} else {
				ft.setCustomAnimations(R.anim.slide_left_in,
						R.anim.slide_right_out);
			}
			ft.replace(R.id.layout_container, fraUserInfoHeight).commit();
		} else if (UserInfoWeightFragment.class.getName().equals(classNameShow)) {
			if (fraUserInfoWeight == null) {
				fraUserInfoWeight = new UserInfoWeightFragment();
			}
			ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_left_out)
					.replace(R.id.layout_container, fraUserInfoWeight).commit();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			long current = System.currentTimeMillis();
			if ((current - back) < 2000) {
				finish();
				System.exit(0);
			} else {
				back = current;
				Toast toast = Toast.makeText(UserInfoImproveActivity.this,
						"再按一次退出程序", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 300);
				toast.show();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
